home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Chess++ ƒ / CChessApp.cp < prev    next >
Text File  |  1993-05-26  |  8KB  |  361 lines

  1. ////////////
  2. //
  3. //    CChessApp.cp
  4. //
  5. //    Application-level methods for Chess++.
  6. //
  7. //    Copyright © 1993 Steven J. Bushell. All rights reserved.
  8. //
  9. ////////////
  10.  
  11. #include <CBartender.h>
  12. #include <Commands.h>
  13. #include "ChessCommands.h"
  14. #include "CChessApp.h"
  15. #include "CChessDoc.h"
  16. #include "CBrain.h"
  17. #include "CAboutChessBox.h"
  18. #include "CChessSplashScreen.h"
  19.  
  20. #include <Lomem.h>
  21.  
  22. extern     void        TimeSpeak(void);
  23.  
  24. extern    CBureaucrat    *gGopher;
  25. extern    CBartender    *gBartender;
  26. extern    OSType        gSignature;
  27. extern    RgnHandle    gChessBoardRgnHandle;
  28. extern    CBrain        *gBrain;
  29.  
  30. extern    CIconHandle    gWhitePawnCicnHandle;
  31. extern    CIconHandle    gWhiteKnightCicnHandle;
  32. extern    CIconHandle    gWhiteBishopCicnHandle;
  33. extern    CIconHandle    gWhiteRookCicnHandle;
  34. extern    CIconHandle    gWhiteQueenCicnHandle;
  35. extern    CIconHandle    gWhiteKingCicnHandle;
  36.  
  37. extern    CIconHandle    gBlackPawnCicnHandle;
  38. extern    CIconHandle    gBlackKnightCicnHandle;
  39. extern    CIconHandle    gBlackBishopCicnHandle;
  40. extern    CIconHandle    gBlackRookCicnHandle;
  41. extern    CIconHandle    gBlackQueenCicnHandle;
  42. extern    CIconHandle    gBlackKingCicnHandle;
  43.  
  44. extern    CursHandle    gCrossCursor;
  45. extern    CursHandle    gPlusCursor;
  46.  
  47. #define        kExtraMasters        4
  48. #define        kRainyDayFund        20480
  49. #define        kCriticalBalance    20480
  50. #define        kToolboxBalance        20480
  51.  
  52.  
  53. /***
  54.  * IChessApp
  55.  *
  56.  *    Initialize the Chess++ application.
  57.  *
  58.  ***/
  59.  
  60. void CChessApp::IChessApp(void)
  61.  
  62. {
  63.     register        rank,file;
  64.     Rect            rankRect;
  65.     CChessSplashScreen    *theSplashScreen;
  66.     long            timer = Ticks;
  67.     
  68.     CApplication::IApplication( kExtraMasters, kRainyDayFund, 
  69.                         kCriticalBalance, kToolboxBalance);
  70.  
  71.     //    show the splash screen
  72.     theSplashScreen = new CChessSplashScreen;
  73.     theSplashScreen->IChessSplashScreen(this);
  74.  
  75.     // initialize checkerboard region for use in CChessBoard::Draw()
  76.     gChessBoardRgnHandle = NewRgn();
  77.     OpenRgn();
  78.     PenSize(1,1);
  79.     SetRect(&rankRect,32,0,64,32);
  80.     for(file=0;file<4;file++)
  81.     {
  82.         for(rank=0;rank<4;rank++)
  83.         {
  84.             FrameRect(&rankRect);
  85.             OffsetRect(&rankRect,-32,32);
  86.             FrameRect(&rankRect);
  87.             OffsetRect(&rankRect,96,-32);
  88.         }
  89.         OffsetRect(&rankRect,-256,64);
  90.     }
  91.     CloseRgn(gChessBoardRgnHandle);
  92.     
  93.     gWhitePawnCicnHandle = GetCIcon(128);
  94.     gWhiteKnightCicnHandle = GetCIcon(129);
  95.     gWhiteBishopCicnHandle = GetCIcon(130);
  96.     gWhiteRookCicnHandle = GetCIcon(131);
  97.     gWhiteQueenCicnHandle = GetCIcon(132);
  98.     gWhiteKingCicnHandle = GetCIcon(133);
  99.  
  100.     gBlackPawnCicnHandle = GetCIcon(134);
  101.     gBlackKnightCicnHandle = GetCIcon(135);
  102.     gBlackBishopCicnHandle = GetCIcon(136);
  103.     gBlackRookCicnHandle = GetCIcon(137);
  104.     gBlackQueenCicnHandle = GetCIcon(138);
  105.     gBlackKingCicnHandle = GetCIcon(139);
  106.  
  107.     // Set up 'normal' idling speed for the board
  108.     cMaxSleepTime = 2;
  109.  
  110.     //    get rid of splash screen after appropriate delay
  111.     while (!Button())
  112.         if (Ticks-timer>333)
  113.             break;
  114.     theSplashScreen->Dispose();
  115.     FlushEvents(mDownMask+mUpMask+keyDownMask+keyUpMask+autoKeyMask, 0);
  116. }
  117.  
  118.  
  119.  
  120. /***
  121.  * SetUpFileParameters
  122.  *
  123.  *    Specify the kinds of files your
  124.  *    application opens.
  125.  *
  126.  ***/
  127.  
  128. void CChessApp::SetUpFileParameters(void)
  129.  
  130. {
  131.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  132.  
  133.         /**
  134.          **    sfNumTypes is the number of file types
  135.          **    your application knows about.
  136.          **    sfFileTypes[] is an array of file types.
  137.          **    You can define up to 4 file types in
  138.          **    sfFileTypes[].
  139.          **
  140.          **/
  141.  
  142.     sfNumTypes = 1;
  143.     sfFileTypes[0] = 'CBRD';
  144.  
  145.         /**
  146.          **    Although it's not an instance variable,
  147.          **    this method is a good place to set the
  148.          **    gSignature global variable. Set this global
  149.          **    to your application's signature. You'll use it
  150.          **    to create a file (see CFile::CreateNew()).
  151.          **
  152.          **/
  153.  
  154.     gSignature = 'SJB3';
  155. }
  156.  
  157.  
  158. /***
  159.  * SetUpMenus 
  160.  *
  161.  * Set up menus which must be created at run time, such as a
  162.  * Font menu. You can eliminate this method if your application
  163.  * does not have any such menus.
  164.  *
  165. ***/
  166.  
  167.  void CChessApp::SetUpMenus()
  168.  {
  169.  
  170.     inherited::SetUpMenus();  /*  Superclass takes care of adding     
  171.                                 menus specified in a MBAR id = 1    
  172.                                 resource    
  173.                             */                          
  174.  
  175.     gBartender->SetDimOption(MENUchess,dimALL);
  176.     
  177.         /* Add your code for creating run-time menus here */    
  178.  }
  179.  
  180.  
  181.  
  182. /***
  183.  * DoCommand
  184.  *
  185.  *    Your application will probably handle its own commands.
  186.  *    Remember, the command numbers from 1-1023 are reserved.
  187.  *  The file Commands.h contains all the predefined TCL
  188.  *  commands.
  189.  *
  190.  *    Be sure to call the default method, so you can get
  191.  *    the default behvior for standard commands.
  192.  *
  193.  ***/
  194. void CChessApp::DoCommand(long theCommand)
  195.  
  196. {
  197.     CAboutChessBox    *theAboutBox;
  198.  
  199.     switch (theCommand) {
  200.     
  201.         case cmdAbout:
  202.             theAboutBox = new CAboutChessBox;
  203.             theAboutBox->IAboutChessBox(this);
  204.             theAboutBox->Dispose();
  205.             FlushEvents(mDownMask+mUpMask+keyDownMask+keyUpMask+autoKeyMask, 0);
  206.             break;
  207.         case cmdTellTime:
  208.             TimeSpeak();
  209.             break;
  210.         default:
  211.             inherited::DoCommand(theCommand);
  212.             break;
  213.     }
  214. }
  215.  
  216.  
  217. /***
  218.  *
  219.  * UpdateMenus 
  220.  *
  221.  *   Perform menu management tasks
  222.  *
  223. ***/
  224.  
  225.  void CChessApp::UpdateMenus()
  226.  {
  227.     inherited::UpdateMenus();     /* Enable standard commands */      
  228.  
  229.     gBartender->EnableCmd(cmdTellTime);
  230. }
  231.  
  232.  
  233. /***
  234.  * Exit
  235.  *
  236.  *    Chances are you won't need this method.
  237.  *    This is the last chance your application gets to clean up
  238.  *  things like temporary files before terminating.
  239.  *
  240.  ***/
  241.  
  242. void CChessApp::Exit()
  243.  
  244. {
  245.     /* your exit handler here */
  246. }
  247.  
  248.  
  249. /***
  250.  * CreateDocument
  251.  *
  252.  *    The user chose New from the File menu.
  253.  *    In this method, you need to create a document and send it
  254.  *    a NewFile() message.
  255.  *
  256.  ***/
  257.  
  258. void CChessApp::CreateDocument()
  259.  
  260. {
  261.     CChessDoc    *theDocument = NULL;
  262.     
  263.     TRY
  264.     {
  265.         theDocument = new(CChessDoc);
  266.             
  267.             /**
  268.              **    Send your document an initialization
  269.              **    message. The first argument is the
  270.              **    supervisor (the application). The second
  271.              **    argument is TRUE if the document is printable.
  272.              **
  273.              **/
  274.         
  275.         theDocument->IChessDoc(this, TRUE);
  276.     
  277.             /**
  278.              **    Send the document a NewFile() message.
  279.              **    The document will open a window, and
  280.              **    set up the heart of the application.
  281.              **
  282.              **/
  283.         theDocument->NewFile();
  284.     }
  285.     
  286.     CATCH
  287.     {
  288.         /*
  289.          * This exception handler gets executed if a failure occurred 
  290.          * anywhere within the scope of the TRY block above. Since 
  291.          * this indicates that a new doc could not be created, we
  292.          * check if an object had been allocated and if it has, send 
  293.          * it a Dispose message. The exception will propagate up to
  294.          * CSwitchboard's exception handler, which handles displaying
  295.          * an error alert.
  296.          */
  297.          
  298.          if (theDocument) theDocument->Dispose();
  299.  
  300.     }
  301.     ENDTRY;
  302. }
  303.  
  304. /***
  305.  * OpenDocument
  306.  *
  307.  *    The user chose Open… from the File menu.
  308.  *    In this method you need to create a document
  309.  *    and send it an OpenFile() message.
  310.  *
  311.  *    The macSFReply is a good SFReply record that contains
  312.  *    the name and vRefNum of the file the user chose to
  313.  *    open.
  314.  *
  315.  ***/
  316.  
  317. void CChessApp::OpenDocument(SFReply *macSFReply)
  318.  
  319. {
  320.     CChessDoc    *theDocument = NULL;
  321.     
  322.     TRY
  323.     {
  324.     
  325.         theDocument = new(CChessDoc);
  326.             
  327.             /**
  328.              **    Send your document an initialization
  329.              **    message. The first argument is the
  330.              **    supervisor (the application). The second
  331.              **    argument is TRUE if the document is printable.
  332.              **
  333.              **/
  334.         
  335.         theDocument->IChessDoc(this, TRUE);
  336.     
  337.             /**
  338.              **    Send the document an OpenFile() message.
  339.              **    The document will open a window, open
  340.              **    the file specified in the macSFReply record,
  341.              **    and display it in its window.
  342.              **
  343.              **/
  344.         theDocument->OpenFile(macSFReply);
  345.     }
  346.     
  347.     CATCH
  348.     {
  349.         /*
  350.          * This exception handler gets executed if a failure occurred 
  351.          * anywhere within the scope of the TRY block above. Since 
  352.          * this indicates that the document could not be opened, we
  353.          * send it a Dispose message. The exception will propagate up to
  354.          * CSwitchboard's exception handler, which handles displaying
  355.          * an error alert.
  356.          */
  357.          
  358.          if (theDocument) theDocument->Dispose();
  359.     }
  360.     ENDTRY;
  361. }